package com.upeoe.lucrocrawler.dao;

import com.upeoe.lucrocrawler.beans.Table;
import com.upeoe.lucrocrawler.utils.DateKit;
import com.upeoe.lucrocrawler.utils.JdbcUtil;
import com.upeoe.lucrocrawler.utils.QueryRunnerWrapper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;

/**
 * @author ven_hxz
 * @create 2016-05-25 上午10:10
 */
public class LucroDaoImpl implements ILucroDao {

    private static final String SAVE_SQL = "INSERT INTO tbl_lucro" +
            "(id, date, transaction, customer, " +
            "level, symbol, volume, commission) " +
            "VALUES(?,?,?,?,?,?,?,?)";

    private static final String HAS_EXIST_SQL = "select count(*) from tbl_lucro where " +
            "date=? and transaction=? and customer=? and level=? " +
            "and symbol=? and volume=? and commission=?";
    private static final String GET_LAST_DATE_SQL = "SELECT * FROM tbl_lucro order by date limit 1";
    private Logger logger = LoggerFactory.getLogger(LucroDaoImpl.class);
    private QueryRunner qr = new QueryRunnerWrapper();

    @Override
    public boolean query(Table table) {
        //throw new RuntimeException("尚未实现");
        try {
            table = qr.query("select * from tbl_lucro where id = ?", new BeanHandler<>(Table.class), table.getId());
            System.out.println(table);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 保存信息
     *
     * @param table
     * @return 1 : 保存成功
     * 0 : 已添加,没有执行保存
     * -1 : 保存失败, 事务回滚
     */
    @Override
    public int save(Table table) {
        table.setId(JdbcUtil.uuid());//设置主键ID

        try {
            if (!hasExist(table)) {
                JdbcUtil.beginTransaction();
                qr.update(SAVE_SQL,
                        table.getId(), table.getDate(), table.getTransaction(), table.getCustomer(),
                        table.getLevel(), table.getSymbol(), table.getVolume(), table.getCommission());
                JdbcUtil.commitTransaction();
                return 1;
            }
            return 0;
        } catch (SQLException var1) {
            try {
                JdbcUtil.rollbackTransaction();
            } catch (SQLException var2) {
                logger.error(var2.getMessage());
                return -1;
            }
            logger.error(var1.getMessage());
            return -1;
        }
    }

    @Override
    public boolean update(Table table) {
        throw new RuntimeException("尚未实现");
    }

    @Override
    public boolean delete(Table table) {
        throw new RuntimeException("尚未实现");
    }

    /**
     * 判断指定记录是否存在
     *
     * @param table
     * @return true : 已存在 ; false : 不存在
     */
    @Override
    public boolean hasExist(Table table) {
        try {
            Number number = qr.query(HAS_EXIST_SQL, new ScalarHandler<>(),
                    table.getDate(), table.getTransaction(), table.getCustomer(), table.getLevel(),
                    table.getSymbol(), table.getVolume(), table.getCommission());
            if (number.intValue() > 0) {
                return true;
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
            return false;
        }
        return false;
    }

    /**
     * 获取数据库最近一条记录的日期
     *
     * @return
     */
    public java.util.Date getLastDate() {
        try {
            Table table = qr.query(GET_LAST_DATE_SQL, new BeanHandler<>(Table.class));
            return DateKit.Timestamp2Date(table.getDate());
        } catch (SQLException e) {
            logger.error(e.getMessage());
        }
        return null;
    }
}
